package com.taoding.controller.base;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.taoding.commons.BaseController;
import com.taoding.commons.CommonsNum;
import com.taoding.entity.base.User;
import com.taoding.service.base.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author lxh123
 * @since 2019-12-04
 */
@RestController
@RequestMapping("/server/user")
@Api(value = "/server/user",description = "用户相关接口")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    @ApiOperation(value = "添加用户")
    public Object add(@RequestBody User user){
        user.setId(IdWorker.getIdStr());
        if (StringUtils.isEmpty(user.getPassword())){ //加密
            user.setPassword(BCrypt.hashpw("123456",BCrypt.gensalt()));//默认密码
        }else {
            user.setPassword(BCrypt.hashpw(user.getPassword(),BCrypt.gensalt()));
        }
        user.setIsDelete(CommonsNum.STATUE_NORMAL);
        user.setCreateTime(DateTime.now().toString(CommonsNum.YYYY_MM_DD_HH_MM_SS));
        user.setUpdateTime(DateTime.now().toString(CommonsNum.YYYY_MM_DD_HH_MM_SS));
       return success(userService.save(user));
    }

}

